package com.woniu.exception.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bean.R;
import com.woniu.exception.bean.*;
import com.woniu.exception.mapper.TExceptionMapper;
import com.woniu.exception.mapper.TTrackMapper;
import com.woniu.exception.service.ITExbriefService;
import com.woniu.exception.service.ITProgressService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-15
 */
@RestController
@RequestMapping("/tExbrief")
public class TExbriefController {

    @Autowired
    private TExceptionMapper tExceptionMapper;

    @Autowired
    private ITExbriefService exbriefService;

    @Autowired
    private TTrackMapper tTrackMapper;

    @Autowired
    private ITProgressService itProgressService;


    @Transactional
    @CrossOrigin
    @GetMapping
    @ApiOperation(value = "派单信息的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3",dataTypeClass = Integer.class),
    })
    public R pageTrack(Integer currentPage, Integer pageSize,Integer exceptionId,Integer dispatchId,String exceptionType){


        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);
        System.out.println("exceptionId = " + exceptionId);
        System.out.println("dispatchId = " + dispatchId);
        System.out.println("exceptionType = " + exceptionType);
        System.out.println("前端搜索框传输数据过来啦！");

        IPage<TExbrief> briefIPage = new Page(currentPage,pageSize);

        IPage<TExbrief> page = exbriefService.page(briefIPage);

        System.out.println("page = " + page);

        List<TExbrief> records = page.getRecords();

        System.out.println("records = " + records);

        //e是owner集合的一个元素，owner对象

        //通过java代码组装 house的操作
        records.forEach(e->{
            TException tException = tExceptionMapper.selectById(e.getExceptionId());
            System.out.println("tException = " + tException);
            e.settException(tException);

        });

        records.forEach(e->{
            TTrack tTrack = tTrackMapper.selectById(e.getTrackId());
            System.out.println("tTrack = " + tTrack);
            e.settTrack(tTrack);
        });

        records.forEach(e->{

            System.out.println("开始处理状态的对象");
            QueryWrapper<TProgress> qr = new QueryWrapper<>();
            qr.eq("progress_id",e.getProgressId());
            TProgress tProgress = itProgressService.getOne(qr);
            System.out.println("tProgress = " + tProgress);
            e.settProgress(tProgress);

        });

        records.forEach(e->{

            System.out.println("开始处理状态的集合");
            List<TProgress> progresses = itProgressService.list();
            System.out.println("progresses = " + progresses);
            e.setProgresses(progresses);


        });


        if (exceptionId!=null){
            records = records.stream().filter(e -> e.getExceptionId() == exceptionId).collect(Collectors.toList());
        }

        if (dispatchId!=null){
            records = records.stream().filter(e->e.gettTrack().getDispatchId()==dispatchId).collect(Collectors.toList());
        }

        if (StringUtils.hasText(exceptionType)){
            records = records.stream().filter(e->e.gettException().getExceptionType().equals(exceptionType)).collect(Collectors.toList());

        }

        long total = page.getTotal();
        System.out.println("total = " + total);

        R ok = R.OK();

        Map<String,Object> map = new HashMap<>();
        map.put("data",records);
        map.put("total",total);

        ok.setData(map);

        return ok;
    }



}
